package com.example.springai.controller;


import com.example.springai.bean.MessageVO;
import com.example.springai.repository.ChatHistoryRepository;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

    @Autowired
    @Qualifier("inDatabaseChatHistoryRepository")
    private ChatHistoryRepository chatHistoryRepository;

    @Autowired
    @Qualifier("inDatabaseChatMemory")
    private ChatMemory chatMemory;



    @GetMapping("/{type}")
    public List<String> getChatIds(@PathVariable String type) {
        return chatHistoryRepository.getChatIds(type);
    }



    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getHistoryByChatId(@PathVariable String type, @PathVariable String chatId) {
        List<Message> messages = chatMemory.get(chatId, 20);
        return messages.stream().map(MessageVO::new).toList();
    }

}
